/**
 * Created by PyCharm.
 * User: Finnur Kolbeinsson
 * Date: 7.2.2012
 * Time: 23:28
 * To change this template use File | Settings | File Templates.
 */
var counter = 2;
var quest_counter = 1;
var working_with = 0;

$(document).ready(function() {
    $('#submitExam').click(function() { // catch the form's submit event

        console.log(quest_counter);
        var runs = 0;
        var questions = new Array();
        while (runs < quest_counter)
        {
            var question = {
                Question:   $('#question'+runs).val(),
                Answer0:    $('#answer'+runs+'_0').val(),
                Answer1:    $('#answer'+runs+'_1').val(),
                Answer2:    $('#answer'+runs+'_2').val(),
                Answer3:    $('#answer'+runs+'_3').val(),
                Answer4:    $('#answer'+runs+'_4').val(),
                Answer5:    $('#answer'+runs+'_5').val(),
                Answer6:    $('#answer'+runs+'_6').val(),
                Answer7:    $('#answer'+runs+'_7').val(),
                Answer8:    $('#answer'+runs+'_8').val(),
                Answer9:    $('#answer'+runs+'_9').val(),
                IsCorrect0: $('#isCorrect'+runs+'_0').attr('checked'),
                IsCorrect1: $('#isCorrect'+runs+'_1').attr('checked'),
                IsCorrect2: $('#isCorrect'+runs+'_2').attr('checked'),
                IsCorrect3: $('#isCorrect'+runs+'_3').attr('checked'),
                IsCorrect4: $('#isCorrect'+runs+'_4').attr('checked'),
                IsCorrect5: $('#isCorrect'+runs+'_5').attr('checked'),
                IsCorrect6: $('#isCorrect'+runs+'_6').attr('checked'),
                IsCorrect7: $('#isCorrect'+runs+'_7').attr('checked'),
                IsCorrect8: $('#isCorrect'+runs+'_8').attr('checked'),
                IsCorrect9: $('#isCorrect'+runs+'_9').attr('checked')
            };
            runs++;
            questions.push(question)
        }

        counter = 2;
        console.log(questions);

        $.post("../../exams/create_question/", questions, function( data )
        {
            alert(data)
        });
    });

    $('#addAnswer').click(function() {

        if(counter > 9){
            alert("Only 10 questions allowed");
            return false;
        }

        var newCreateAnswer = $(document.createElement('div'))
            .attr("id", '' + 'createAnswer' + counter + '');

        newCreateAnswer.html('' +
            '<label name="answer' + counter +'">Answer #'+ (counter+1) + ' : </label>' +
            '<input type="text" name="answer' + counter + '" id="answer' + counter + '" value="" size="100"/>' +
            '<input type="checkbox" name="isCorrect' + counter + '" id="isCorrect' + counter + '"/>' +
            '');

        var div = ''+'"#createAnswerGroup' + (quest_counter-1) +'_'+ (counter-2) + '"';
        console.log (div);
        newCreateAnswer.appendTo(div);


        counter++;

    });

    $('#addQuestion').click(function() {
        //counter = 0;

        if(quest_counter > 49){
            alert("Only 50 questions allowed");
            return false;
        }

        var newCreateQuestion = $(document.createElement('fieldset'))
            .attr("id", 'qa' + quest_counter);

//        newCreateQuestion.html('' +
//            '<label name="answer' + counter +'">Answer #'+ (counter+1) + ' : </label>' +
//            '<input type="text" name="answer' + counter + '" id="answer' + counter + '" value="" size="100"/>' +
//            '<input type="checkbox" name="isCorrect' + counter + '" id="isCorrect' + counter + '"/>' +
//            '');



        newCreateQuestion.html('' + '<div id="createQuestion' + quest_counter +'">'+
                '<label for="question' + quest_counter +'" >Question ' + (quest_counter+1) +' : </label>'+
                '<input type="text" id="question' + quest_counter +'" size="100"/>'
            +'</div>'+'')

        for(counter = 0; counter < 10; counter++)
        {
            newCreateQuestion.append(
                '<div id="createAnswer' + quest_counter +'_'+ counter +'">'+
                    '<label for="answer' + counter + '" >Answer #' + (counter+1) + ' : </label>'+
                    '<input type="text" id="answer' + quest_counter +'_'+ counter +'" size="100"/>'+
                    '<input type="checkbox" id="isCorrect' + quest_counter +'_'+ counter +'" name="isCorrect' + quest_counter +'_'+ counter +'"/>'+
                '</div>'+
        '');
        }
        newCreateQuestion.appendTo("#allQuestions");

        counter=2
        quest_counter++;

    });

    $(document).ajaxSend(function(event, xhr, settings) {
        function getCookie(name) {
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }
        function sameOrigin(url) {
            // url could be relative or scheme relative or absolute
            var host = document.location.host; // host + port
            var protocol = document.location.protocol;
            var sr_origin = '//' + host;
            var origin = protocol + sr_origin;
            // Allow absolute or scheme relative URLs to same origin
            return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
                (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
                // or any other URL that isn't scheme relative or absolute i.e relative.
                !(/^(\/\/|http:|https:).*/.test(url));
        }
        function safeMethod(method) {
            return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
        }

        if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
            xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
        }
    });

});